/*
<table class="multi-select">
	<tr>
		<td>
			<select multiple="multiple" ondblclick="multiSelect.add()" >
				<option ng-repeat="item in persons" value="{name:'{{item.name}}',id:{{item.id}}}">{{item.name}}</option>
			</select>
		</td>
		<td>
			<button type="button" onclick="multiSelect.add()">&nbsp;&gt;&nbsp;</button><br />
			<button type="button" onclick="multiSelect.remove()">&nbsp;&lt;&nbsp;</button><br />
			<button type="button" onclick="multiSelect.addAll()">&gt;&gt;</button><br />
			<button type="button" onclick="multiSelect.removeAll()">&lt;&lt;</button><br />
		</td>
		<td>
			<select id="xj-selected" multiple="multiple" ondblclick="multiSelect.remove()">
			</select>
		</td>
	</tr>
</table>
*/
MultiSelect = function(options){
	this.init();
};
MultiSelect.prototype = {
	options:{
		element:".multi-select"
	},
	source:null,
	target:null,
	init:function(){
		this.source = $(this.options.element).find("select").eq(0);
		this.target = $(this.options.element).find("select").eq(1);
	},
	add:function(){
		var dom = this.source.get(0);
		var index = dom.selectedIndex;
		var value = this.source.val();
		var valueObj = eval('('+value+')');
		var option = document.createElement("option");
		var text = document.createTextNode(valueObj.name);
		option.setAttribute("value",value);
		option.appendChild(text);

		this.target.prepend(option);
		this.source.find("option").eq(index).remove();
	},
	remove:function(){
		var dom = this.target.get(0);
		var index = dom.selectedIndex;
		var value = this.target.val();
		var valueObj = eval('('+value+')');
		var option = document.createElement("option");
		var text = document.createTextNode(valueObj.name);
		option.setAttribute("value",value);
		option.appendChild(text);
		
		this.source.prepend(option);
		this.target.find("option").eq(index).remove();
	},
	addAll:function(){
		this.target.prepend(this.source.children());
	},
	removeAll:function(){
		this.source.prepend(this.target.children());
	},
	getSelected:function(){
		var items = [];
		this.target.children().each(function(){
			items.push(eval("("+$(this).val()+")"));
		});
		return items;
	}
};